clear;close all;clc;

% we read in our single-source recordings to create our own models at 4
% frequencies.

Oldmic0_3Dio1 = 1;

rad=0.1;
Norder=3;
thre = 0.0;

% tdur=0.04; % formal
% tdur=0.08; % formal
if Oldmic0_3Dio1==0
    % freqs = [200 300 500 600 800 1000 1200 1500 2000 3000];
    % nturns=[0.1 0.2 0.3 0.4 0.5 0.8 1 1.2 1.5 2];
    freqs= [ 850 900 1000]; %just a random frequency
    nturns = [0.98 1.09 1.13];
else
    onsets = [14 18.5 12.5 15 12.5 21.5 13 12.5 12 12.5 ...
        12 12.5 12.5 12.5 15 12 12 12 12]
    freqs = 100:100:3000
    nturns=[linspace(0.2, 1, 6) linspace(1.2, 2.3,24)];
end

fs = 44100;
% whos
plot_each =1;



Angles = -90:10:90;
sides = {'Left','Right'};


f2=figure('position',[100 70 800 500]);

% for i=1:10
%     ff(i)=figure;
% end

for ifreql = 1:length(freqs)
    % for ifreql = 1
    f1=figure('position',[100 70 1600 700]);
    f3=figure('position',[100 70 1600 700]);
    freq = freqs(ifreql)
    % tdur = 20/freq;
    tdur = 40/freq;
    for iangle = 1:length(Angles)

        % cc = 0;
        Angle = Angles(iangle);
        if Oldmic0_3Dio1==0
            Num_centers = 1;
            Proto_Num = 1.2;
            % Proto_Num = 21;
            PString = num2str(Proto_Num);
            if Angle < 0
                eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
                % eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_' sides{1} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
            elseif Angle ==0
                eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_'    num2str(abs(Angles(iangle))) 'degree.mat']);
                % eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_'  num2str(Angles(iangle)) 'degree.mat']);
            else
                eval(['load All_sound_recordings\Protocol_' PString(1) '\Protocol_' PString '_' sides{2} '_'   num2str(abs(Angles(iangle))) 'degree.mat']);
                % eval(['load All_sound_recordings\Protocol_' PString '\Phoneme_' PString '_' sides{1} '_'   num2str(Angles(iangle)) 'degree.mat']);
            end
            recorded_sound=recorded_sound(fs:end,:);
        else
            Angle = Angles(iangle);
            Proto_Num=32;
            % Proto_Num=2;
            PString = num2str(Proto_Num);
            if Angle < 0
                ss = '-';
            else
                ss = '+';
            end
            [YY fs2]=audioread(['C:\Users\gaiy\OneDrive - Saint Louis University\Sound_loc\3Dio recordings\Protocol_' PString '_deg' ss num2str(abs(Angles(iangle))) '.mp3']);
            
            if Proto_Num==32
                YY=YY(1:end/10,:);
                recorded_sound=resample(YY,441,320);
                % % figure(f3)
                % % subplot(3,7,iangle);
                % % plot( recorded_sound(:,1));hold on;
                period = 10;
                beginP = 1+fs;
                endP = (period-2).*fs; % the first period is BB noise
                recorded_sound=recorded_sound(onsets(iangle).*fs+beginP:onsets(iangle).*fs+endP,:);
                
                % % plot((onsets(iangle).*fs+beginP).*[1 1],[-1 1],'b');
                % % plot((onsets(iangle).*fs+endP).*[1 1],[-1 1],'r');
                % % pause(0.2)
            else
                recorded_sound=resample(YY,441,320);
            end

        end

        left_sound = recorded_sound(:,1);
        right_sound = recorded_sound(:,2);
        [SL,SR,freq_pos,flag]=mySTFT(left_sound,right_sound,fs,thre,tdur);
        % normalization:
        % [SL1,SR1]=Normalize(SL,SR);
        % if Angle < 0
        %     L_or_R=1;
        % else
        L_or_R=2;
        % end
        SLR=Normalize_Both(SL,SR,L_or_R);
        [Y Ind] = min(abs(freq_pos - freq));
        X_P = [real(SLR(Ind,flag>0))' imag(SLR(Ind,flag>0))'];
        figure(f1)
        subplot(3,7,iangle);
        plot(X_P(:,1),X_P(:,2),'k.');hold on;axis square
        % end

        out = f_mycenter(X_P,rad);
        F_centers(iangle,:) = out.C1;

        s=plot(F_centers(iangle,1),F_centers(iangle,2),'go');hold on;set(s,'linewidth',2)
        %         grid on
        set(gca,'Xtick',-1:0.5:1)
        set(gca,'Ytick',-1:0.5:1)
        if Angle < 0
            Pangle =['-' num2str(abs(Angles(iangle))) ];
        elseif Angle ==0
            Pangle =[num2str(abs(Angles(iangle))) ];
        else
            Pangle =['+' num2str(abs(Angles(iangle))) ];
        end
        title([num2str(freq) 'Hz;' Pangle]);
        axis([-1 1 -1 1])

        figure(f2)
        % subplot(2,3,ifreql)
        % subplot(2,5,ifreql)
        subplot(5,6,ifreql)
        % figure(ff(ifreql))
        s=text(F_centers(iangle,1),F_centers(iangle,2),Pangle);hold on;set(s,'fontsize',7)
        hold on;
        axis([-1 1 -1 1])
        % pause
        pause(0.01)
    end % angle

    hold on;
    plot(-1:1,[0,0,0],'k', 'LineWidth', 0.5);
    plot([0,0,0],-1:1,'k','LineWidth',0.5)
    set(gca,'fontsize',10)
    xlabel('Re','fontsize',12)
    ylabel('Im','fontsize',12)
    hold on

    figure(f2)
    % figure(ff(ifreql))
    % subplot(2,3,ifreql)
    % subplot(2,5,ifreql)
    subplot(5,6,ifreql)
    plot(F_centers(:,1),F_centers(:,2),'r')
    title([num2str(freq) ' Hz'])
    axis([-1 1 -1 1]);axis square
    %==============================
    out=f_get_spiral(F_centers,Norder,nturns(ifreql));
    x_spiral=out.x_spiral;
    y_spiral=out.y_spiral;
    % plot(x_spiral,y_spiral,'color',[0.5 0.5 0.5]) ; % nturns crossings, including end point
    % subplot(2,5,ifreql)
    figure(f2)
    subplot(5,6,ifreql)
    plot(x_spiral,y_spiral,'color','g') ; % nturns crossings, including end point

    axis square
    pause(0.2)
    % eval(['save Freq_centers_own' num2str(freq) ' F_centers  x_spiral y_spiral'])
    clear F_centers
end % freq




% figure;
% pc = 'bkrm';
% for ifreq = 1:4
%     s=plot([fliplr(-Angles) Angles],[fliplr(record_class{1}(ifreq,:)) record_class{2}(ifreq,:)]',pc(ifreq));hold on;
%     set(s,'linewidth',2)
%     if ifreq<3
%         set(s,'linestyle',':')
%     end
% end
% plot([-100 100],[-100 100],'g');
% legend('300 Hz',' 600 Hz',' 1500 Hz',' 3000 Hz','Perfect','Location','northwest')
% legend boxoff
% xlabel('True Angle (degrees)','fontsize',12)
% ylabel('classified Angle (degrees)','fontsize',12)
% title('Single Source')
